package com.nju.edu.erp.web.controller;

import com.nju.edu.erp.auth.Authorized;
import com.nju.edu.erp.enums.Role;
import com.nju.edu.erp.enums.sheetState.ReceiptSheetState;
import com.nju.edu.erp.model.vo.UserVO;
import com.nju.edu.erp.model.vo.receipt.ReceiptSheetVO;
import com.nju.edu.erp.service.finance.FinanceReceiptService;
import com.nju.edu.erp.web.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping(path = "/finance-receipt")
public class  FinanceReceiptController {

    private final FinanceReceiptService financeReceiptService;

    @Autowired
    public FinanceReceiptController(FinanceReceiptService financeReceiptService){
        this.financeReceiptService=financeReceiptService;
    }

    @Authorized (roles = {Role.FINANCIAL_STAFF, Role.GM, Role.ADMIN})
//    @PostMapping("/sheet")
    @PostMapping(value = "/sheet-make1")
    public Response makeReceiptOrder(@RequestBody ReceiptSheetVO receiptSheetVO, UserVO userVO){
        financeReceiptService.makeReceiptSheet(userVO,receiptSheetVO);
        return Response.buildSuccess();
    }

    @Authorized (roles = {Role.GM, Role.ADMIN})
    @GetMapping(value = "/approval")
    public Response Approval(@RequestParam("receiptSheetId") String receiptSheetId,
                                   @RequestParam("state") ReceiptSheetState state)  {
        if(state.equals(ReceiptSheetState.FAILURE) || state.equals(ReceiptSheetState.SUCCESS)) {
            financeReceiptService.approval(receiptSheetId, state);
            return Response.buildSuccess();
        } else {
            return Response.buildFailed("000000","操作失败"); // code可能得改一个其他的
        }
    }

    @GetMapping(value = "/sheet-show")
    public Response showSheetByState(@RequestParam(value = "state", required = false) ReceiptSheetState state)  {
        return Response.buildSuccess(financeReceiptService.getReceiptSheetByState(state));
    }

}
